<?php
namespace App\Service\Admin;

class CateService extends BaseService
{
    //商城商品

    public static function index($type)
    {
        $db = self::dbConn(self::$dbCate)->where('del', 0);
        $db = $db->where('type', $type);
        $total = $db->count();
        $res = $db->orderBy('id', 'asc')->get();
        if (count($res)) {
            foreach ($res as &$v) {
                $v->icon = file_exists($v->icon) ? env('DOMAIN') . '/' . $v->icon : '';
            }
        }
        return self::toback(0, '', $res, $total);
    }

    /**
     * 新增
     * @param array $pas
     */
    public static function add($pas)
    {
        $pas['uptime'] = $pas['addtime'] = toDate();
        $res = self::dbConn(self::$dbCate)->insertGetId($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    /**
     * 修改
     * @param int $id
     * @param array $pas
     */
    public static function modify($id, $pas)
    {
        $pas['uptime'] = toDate();
        $up = self::dbConn(self::$dbCate)
            ->where('id', $id)
            ->update($pas);
        if (!$up) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    /**
     * 删除
     * @param id $id 场景ID
     * return array
     */
    public static function del($id)
    {
        $pas = [
            'del' => 1,
        ];
        $res = self::dbConn(self::$dbCate)
            ->where('id', $id)
            ->update($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }
}